clear
cd ""		//Set your directory here

******************************
***GET AND CLEAN DATA FIRST***
******************************

use ".dta"		//Provide the name of the CCS wave 1 dataset here. Data from the CCS are available upon request in the online system FORSbase.
set more off

	*append CCS wave 2
		append using ".dta"		//Provide the name of the CCS wave 2 dataset here. Data from the CCS are available upon request in the online system FORSbase.
		
		gen wave = 1 if t0 ~= .			//wave indicator
		replace wave = 2 if T0 ~= .		//wave indicator
		
		replace a1 = A1 if a1 == .		//party number		
		
		replace t1 = 2 if T1 == 2		//get country number from survey 2 (Switzerland)
		replace t1 = 5 if T1 == 4		//get country number from survey 2 (Greece)
		replace t1 = 19 if T1 == 7		//get country number from survey 2 (Norway)
		replace t1 = 12 if T1 == 12		//get country number from survey 2 (Iceland)
		replace t1 = 6 if T1 == 15		//get country number from survey 2 (Finland)
		replace t1 = 10 if T1 == 16		//get country number from survey 2 (Portugal)
		replace t1 = 12 if T1 == 19		//get country number from survey 2 (Iceland 2016)
		replace t1 = 12 if T1 == 20		//get country number from survey 2 (Iceland 2017)
		
		replace t3 = T3 if t3 == .		//year
		
		replace t0 = T0 if t0 ==.		//candidate identifier

	*drop observations we don't need
		drop if t1 == . 
	
	*MP indicator
		replace a4b1 = A4b1 if a4b1 == .	
	
	*get countrynames
		gen countryname = ""
		replace countryname = "Switzerland" if t1 == 2
		replace countryname = "Greece" if t1 == 5 
		replace countryname = "Finland" if t1 == 6 
		replace countryname = "Netherlands" if t1 == 8 
		replace countryname = "Portugal" if t1 == 10 
		replace countryname = "Iceland" if t1 == 12 
		replace countryname = "Austria" if t1 == 14 
		replace countryname = "Denmark" if t1 == 16 
		replace countryname = "Norway" if t1 == 19 
		replace countryname = "Italy" if t1 == 21 
		replace countryname = "Great Britain" if t1 == 23		
	
	*get seniority variable
		gen seniority = . 
		replace seniority = 1 if a4b1 == 1 
		replace seniority = 2 if a4b2 == 1
		replace seniority = 3 if a4b3 == 1 & a4b2 == 1
		replace seniority = 4 if a4b4 == 1 & a4b3 == 1 & a4b2 == 1
		replace seniority = 5 if a4b5 == 1 & a4b4 == 1 & a4b3 == 1 & a4b2 == 1
		replace seniority = 6 if a4b6 == 1 & a4b5 == 1 & a4b4 == 1 & a4b3 == 1 & a4b2 == 1
		
		replace seniority = . if a4b2 == .
		replace seniority = . if a4b3 == .
		replace seniority = . if a4b4 == .
		replace seniority = . if a4b5 == .
		replace seniority = . if a4b6 == .
		
		*from second wave
			replace seniority = 1 if A4b1 == 1
			replace seniority = 2 if A4b2 == 1
			replace seniority = 3 if A4b3 == 1 & A4b2 == 1
			replace seniority = 4 if A4b4 == 1 & A4b3 == 1 & A4b2 == 1
			replace seniority = 5 if A4b5 == 1 & A4b4 == 1 & A4b3 == 1 & A4b2 == 1
			replace seniority = 6 if A4b6 == 1 & A4b5 == 1 & A4b4 == 1 & A4b3 == 1 & A4b2 == 1
				
				
			replace seniority = . if A4b2 == . & wave == 2
			replace seniority = . if A4b3 == . & wave == 2
			replace seniority = . if A4b4 == . & wave == 2
			replace seniority = . if A4b5 == . & wave == 2
			replace seniority = . if A4b6 == . & wave == 2
			 
	*get left-right variable
		gen leftright = c3						//Wave 1
		replace leftright = C3a if c3 == .		//wave 2

	*get government variable
		gen government = 0
				
		*Austria
			replace government = 1 if countryname == "Austria" & a1 == 1 & t3 == 2008		//SPO
			replace government = 1 if countryname == "Austria" & a1 == 2 & t3 == 2008		//OVP
				
		*Denmark
			replace government = 1 if countryname == "Denmark" & a1 == 7 & t3 == 2011		//Venstre
			replace government = 1 if countryname == "Denmark" & a1 == 8 & t3 == 2011		//Konservative
		
		*Finland
			replace government = 1 if countryname == "Finland" & a1 == 1 & t3 == 2007		//KESK
			replace government = 1 if countryname == "Finland" & a1 == 3 & t3 == 2007		//SSDP 
			replace government = 1 if countryname == "Finland" & a1 == 7 & t3 == 2007		//RKP-SFP

			replace government = 1 if countryname == "Finland" & a1 == 4 & t3 == 2011		//KESK
			replace government = 1 if countryname == "Finland" & a1 == 1 & t3 == 2011		//KOK
			replace government = 1 if countryname == "Finland" & a1 == 6 & t3 == 2011		//VIHR
			replace government = 1 if countryname == "Finland" & a1 == 8 & t3 == 2011		//RKP-SFP

			replace government = 1 if countryname == "Finland" & a1 == 1 & t3 == 2015		//KOK
			replace government = 1 if countryname == "Finland" & a1 == 2 & t3 == 2015		//SSDP
			replace government = 1 if countryname == "Finland" & a1 == 8 & t3 == 2015		//RKP-SFP
			replace government = 1 if countryname == "Finland" & a1 == 7 & t3 == 2015		//KD
			
		*Great Britain
			replace government = 1 if countryname == "Great Britain" & a1 == 1 & t3 == 2010		//Labour
		
		*Greece
			replace government = 1 if countryname == "Greece" & a1 == 1 & t3 == 2007		//ND
			
			replace government = 1 if countryname == "Greece" & a1 == 1 & t3 == 2009		//ND
						
			replace government = 1 if countryname == "Greece" & a1 == 2 & t3 == 2015		//ND
			replace government = 1 if countryname == "Greece" & a1 == 7 & t3 == 2015		//PASOK
		
		*Iceland
			replace government = 1 if countryname == "Iceland" & a1 == 1 & t3 == 2009		//Social democratic alliance
			replace government = 1 if countryname == "Iceland" & a1 == 4 & t3 == 2009		//Greens
	
			replace government = 1 if countryname == "Iceland" & a1 == 1 & t3 == 2013		//Social democratic alliance
			replace government = 1 if countryname == "Iceland" & a1 == 4 & t3 == 2013		//Greens

			replace government = 1 if countryname == "Iceland" & a1 == 2 & t3 == 2016		//F
			replace government = 1 if countryname == "Iceland" & a1 == 3 & t3 == 2016		//Sj

			replace government = 1 if countryname == "Iceland" & a1 == 3 & t3 == 2017		//Sj
			replace government = 1 if countryname == "Iceland" & a1 == 9 & t3 == 2017		//V			
			replace government = 1 if countryname == "Iceland" & a1 == 5 & t3 == 2017		//BF
		
		*Italy (caretaker)
		
		*Netherlands
			replace government = 1 if countryname == "Netherlands" & a1 == 1 & t3 == 2006		//CDA
			replace government = 1 if countryname == "Netherlands" & a1 == 4 & t3 == 2006		//VVD	
			replace government = 1 if countryname == "Netherlands" & a1 == 8 & t3 == 2006		//D66
		
		*Norway
			replace government = 1 if countryname == "Norway" & a1 == 2 & t3 == 2009			//DNA
			replace government = 1 if countryname == "Norway" & a1 == 1 & t3 == 2009			//SV
			replace government = 1 if countryname == "Norway" & a1 == 3 & t3 == 2009			//Sp
			
			replace government = 1 if countryname == "Norway" & a1 == 3 & t3 == 2013			//DNA
			replace government = 1 if countryname == "Norway" & a1 == 2 & t3 == 2013			//SV
			replace government = 1 if countryname == "Norway" & a1 == 4 & t3 == 2013			//Sp

		*Portugal
			replace government = 1 if countryname == "Portugal" & a1 == 5 & t3 == 2009			//PS 
			
			replace government = 1 if countryname == "Portugal" & a1 == 6 & t3 == 2011			//PS 

			replace government = 1 if countryname == "Portugal" & a1 == 2 & t3 == 2015			//CDS-PP.PPM
			replace government = 1 if countryname == "Portugal" & a1 == 5 & t3 == 2015			//PPD/PSD
			replace government = 1 if countryname == "Portugal" & a1 == 6 & t3 == 2015			//PPD/PSD-CDS-PP

		*Switzerland
			replace government = 1 if countryname == "Switzerland" & a1 == 3 & t3 == 2007		//SVP-UDC 
			replace government = 1 if countryname == "Switzerland" & a1 == 4 & t3 == 2007		//SP-PS 
			replace government = 1 if countryname == "Switzerland" & a1 == 2 & t3 == 2007		//FDP-PRD 
			replace government = 1 if countryname == "Switzerland" & a1 == 1 & t3 == 2007		//CVP-PDC 
			
			replace government = 1 if countryname == "Switzerland" & a1 == 4 & t3 == 2011		//SVP-UDC 
			replace government = 1 if countryname == "Switzerland" & a1 == 3 & t3 == 2011		//SP-PS
			replace government = 1 if countryname == "Switzerland" & a1 == 1 & t3 == 2011		//FDP-PRD 
			replace government = 1 if countryname == "Switzerland" & a1 == 2 & t3 == 2011		//CVP-PDC 	
			replace government = 1 if countryname == "Switzerland" & a1 == 19 & t3 == 2011		//BDP 

			replace government = 1 if countryname == "Switzerland" & a1 == 4 & t3 == 2015		//SVP-UDC 
			replace government = 1 if countryname == "Switzerland" & a1 == 3 & t3 == 2015		//SP-PS 
			replace government = 1 if countryname == "Switzerland" & a1 == 1 & t3 == 2015		//FDP 
			replace government = 1 if countryname == "Switzerland" & a1 == 2 & t3 == 2015		//CVP-PDC 
			replace government = 1 if countryname == "Switzerland" & a1 == 32 & t3 == 2015		//BDP 	
	
	*merge in country level electoral rules and clientelism
		merge m:1 countryname t3 using "Electoral rules and clientelism CCS"
		drop _merge

		replace personalism = 1 if countryname == "Denmark" & a1 == 1 					//Unity List list uses a de facto closed list in all districts
		replace closed = 1 if countryname == "Denmark" & a1 == 1 						//Unity List list uses a de facto closed list in all districts
		replace personalism = 1 if countryname == "Denmark" & a1 == 2 & t6 == 2			//Socialist People's Party uses a de facto closed list in some districts 
		replace personalism = 1 if countryname == "Denmark" & a1 == 2 & t6 == 7			//Socialist People's Party uses a de facto closed list in some districts 
		replace personalism = 1 if countryname == "Denmark" & a1 == 2 & t6 == 8			//Socialist People's Party uses a de facto closed list in some districts 
		replace closed = 1 if countryname == "Denmark" & a1 == 2 & t6 == 2				//Socialist People's Party uses a de facto closed list in some districts 
		replace closed = 1 if countryname == "Denmark" & a1 == 2 & t6 == 7				//Socialist People's Party uses a de facto closed list in some districts 
		replace closed = 1 if countryname == "Denmark" & a1 == 2 & t6 == 8				//Socialist People's Party uses a de facto closed list in some districts 
		
	*merge in coalition variable
		merge m:1 t1 t3 a1 using "Coalition dataset CCS"
		
	*reverse scale for DV	
		replace b10c = 6-b10c		
		replace B7d = 6-B7d	
		gen pork = b10c if wave == 1
		replace pork = B7d if wave == 2
	
	*get district magnitude in countries from Wave 2 (provided in wave 1 except for SMD countries)
			replace t10 = . if t10 == .a
			replace t10 = . if t10 == .b
			
			*Switzerland 
				replace t10 = 35 if countryname == "Switzerland" & T6 == 1	//1 Zürich
				replace t10 = 25 if countryname == "Switzerland" & T6 == 2	//2 Bern
				replace t10 = 10 if countryname == "Switzerland" & T6 == 3	//3 Luzern
				replace t10 = 1 if countryname == "Switzerland" & T6 == 4	//4 Uri
				replace t10 = 4 if countryname == "Switzerland" & T6 == 5	//5 Schwyz
				replace t10 = 1 if countryname == "Switzerland" & T6 == 6	//6 Obwalden
				replace t10 = 1 if countryname == "Switzerland" & T6 == 7	//7 Nidwalden
				replace t10 = 1 if countryname == "Switzerland" & T6 == 8	//8 Glarus
				replace t10 = 3 if countryname == "Switzerland" & T6 == 9	//9 Zug
				replace t10 = 7 if countryname == "Switzerland" & T6 == 10	//10 Fribourg
				replace t10 = 6 if countryname == "Switzerland" & T6 == 11	//11 Solothurn
				replace t10 = 5 if countryname == "Switzerland" & T6 == 12	//12 Basel-Stadt
				replace t10 = 7 if countryname == "Switzerland" & T6 == 13	//13 Basel-Land
				replace t10 = 2 if countryname == "Switzerland" & T6 == 14	//14 Schaffhausen
				replace t10 = 1 if countryname == "Switzerland" & T6 == 15	//15 Appenzell-Ausserrhoden
				replace t10 = 1 if countryname == "Switzerland" & T6 == 16	//16 Appenzell-Innerrhoden
				replace t10 = 12 if countryname == "Switzerland" & T6 == 17	//17 St.Gallen
				replace t10 = 5 if countryname == "Switzerland" & T6 == 18	//18 Graubünden
				replace t10 = 16 if countryname == "Switzerland" & T6 == 19	//19 Aargau
				replace t10 = 6 if countryname == "Switzerland" & T6 == 20	//20 Thurgau
				replace t10 = 8 if countryname == "Switzerland" & T6 == 21	//21 Ticino
				replace t10 = 18 if countryname == "Switzerland" & T6 == 22	//22 Vaud
				replace t10 = 8 if countryname == "Switzerland" & T6 == 23	//23 Valais
				replace t10 = 4 if countryname == "Switzerland" & T6 == 24	//24 Neuchatel
				replace t10 = 11 if countryname == "Switzerland" & T6 == 25	//25 Geneve
				replace t10 = 2 if countryname == "Switzerland" & T6 == 26	//26 Jura	
				
			*Switzerland (upper house) 
				replace t10 = 2 if countryname == "Switzerland" & T6 == 1 & T7 == 2	//1 Zürich
				replace t10 = 2 if countryname == "Switzerland" & T6 == 2 & T7 == 2	//2 Bern
				replace t10 = 2 if countryname == "Switzerland" & T6 == 3 & T7 == 2	//3 Luzern
				replace t10 = 2 if countryname == "Switzerland" & T6 == 4 & T7 == 2		//4 Uri
				replace t10 = 2 if countryname == "Switzerland" & T6 == 5 & T7 == 2		//5 Schwyz
				replace t10 = 1 if countryname == "Switzerland" & T6 == 6 & T7 == 1		//6 Obwalden
				replace t10 = 1 if countryname == "Switzerland" & T6 == 7 & T7 == 1		//7 Nidwalden
				replace t10 = 2 if countryname == "Switzerland" & T6 == 8 & T7 == 2		//8 Glarus
				replace t10 = 2 if countryname == "Switzerland" & T6 == 9 & T7 == 2		//9 Zug
				replace t10 = 2 if countryname == "Switzerland" & T6 == 10 & T7 == 2	//10 Fribourg
				replace t10 = 2 if countryname == "Switzerland" & T6 == 11 & T7 == 2	//11 Solothurn
				replace t10 = 1 if countryname == "Switzerland" & T6 == 12 & T7 == 1	//12 Basel-Stadt
				replace t10 = 1 if countryname == "Switzerland" & T6 == 13 & T7 == 1	//13 Basel-Land
				replace t10 = 2 if countryname == "Switzerland" & T6 == 14 & T7 == 2	//14 Schaffhausen
				replace t10 = 1 if countryname == "Switzerland" & T6 == 15 & T7 == 1	//15 Appenzell-Ausserrhoden
				replace t10 = 1 if countryname == "Switzerland" & T6 == 16 & T7 == 1	//16 Appenzell-Innerrhoden
				replace t10 = 2 if countryname == "Switzerland" & T6 == 17 & T7 == 2	//17 St.Gallen
				replace t10 = 2 if countryname == "Switzerland" & T6 == 18 & T7 == 2	//18 Graubünden
				replace t10 = 2 if countryname == "Switzerland" & T6 == 19 & T7 == 2	//19 Aargau
				replace t10 = 2 if countryname == "Switzerland" & T6 == 20 & T7 == 2	//20 Thurgau
				replace t10 = 2 if countryname == "Switzerland" & T6 == 21 & T7 == 2	//21 Ticino
				replace t10 = 2 if countryname == "Switzerland" & T6 == 22 & T7 == 2	//22 Vaud
				replace t10 = 2 if countryname == "Switzerland" & T6 == 23 & T7 == 2	//23 Valais
				replace t10 = 2 if countryname == "Switzerland" & T6 == 24 & T7 == 2	//24 Neuchatel
				replace t10 = 2 if countryname == "Switzerland" & T6 == 25 & T7 == 2	//25 Geneve
				replace t10 = 2 if countryname == "Switzerland" & T6 == 26 & T7 == 2	//26 Jura	
			
				drop if countryname == "Switzerland" & t9 == 3		//Respondents who run for both upper and lower house 
				drop if countryname == "Switzerland" & T7 == 3		//Respondents who run for both upper and lower house 
					
			*Greece	
				replace t10 = 4 if countryname == "Greece" & T6 == 1		//1 EVROU	
				replace t10 = 3 if countryname == "Greece" & T6 == 2		//2 RODOPIS		
				replace t10 = 3 if countryname == "Greece" & T6 == 3		//3 XANTHIS		
				replace t10 = 3 if countryname == "Greece" & T6 == 4		//4 DRAMAS		
				replace t10 = 4 if countryname == "Greece" & T6 == 5		//5 KAVALAS
				replace t10 = 16 if countryname == "Greece" & T6 == 6		//6 A THESSALONIKI		
				replace t10 = 9 if countryname == "Greece" & T6 == 7		//7 B THESSALONIKI		
				replace t10 = 6 if countryname == "Greece" & T6 == 8		//8 SERRON		
				replace t10 = 3 if countryname == "Greece" & T6 == 9		//9 CHALKIDIKIS
				replace t10 = 3 if countryname == "Greece" & T6 == 10		//10 KILKIS
				replace t10 = 4 if countryname == "Greece" & T6 == 11		//11 PELLIS
				replace t10 = 4 if countryname == "Greece" & T6 == 12		//12 IMATHIAS
				replace t10 = 4 if countryname == "Greece" & T6 == 13		//13 PIERIAS
				replace t10 = 2 if countryname == "Greece" & T6 == 14		//14 FLORINIS
				replace t10 = 5 if countryname == "Greece" & T6 == 15		//15 KOZANIS
				replace t10 = 2 if countryname == "Greece" & T6 == 16		//16 KASTORIAS
				replace t10 = 1 if countryname == "Greece" & T6 == 17		//17 GREVENON
				replace t10 = 5 if countryname == "Greece" & T6 == 18		//18 IOANNINON
				replace t10 = 3 if countryname == "Greece" & T6 == 19		//19 ARTIS
				replace t10 = 2 if countryname == "Greece" & T6 == 20		//20 THESPROTIAS
				replace t10 = 2 if countryname == "Greece" & T6 == 21		//21 PREVEZAS
				replace t10 = 8 if countryname == "Greece" & T6 == 22		//22 LARISAS
				replace t10 = 6 if countryname == "Greece" & T6 == 23		//23 MAGNISSIAS
				replace t10 = 4 if countryname == "Greece" & T6 == 24		//24 TRIKALON
				replace t10 = 4 if countryname == "Greece" & T6 == 25		//25 KARDITSAS
				replace t10 = 3 if countryname == "Greece" & T6 == 26		//26 KERKYRAS (Korfu)
				replace t10 = 1 if countryname == "Greece" & T6 == 27		//27 LEFKADOS	
				replace t10 = 1 if countryname == "Greece" & T6 == 28		//28 KEFALONIAS
				replace t10 = 1 if countryname == "Greece" & T6 == 29		//29 ZAKYNTHOY (Zante)
				replace t10 = 7 if countryname == "Greece" & T6 == 30		//30 AETOLOAKARNANIAS
				replace t10 = 8 if countryname == "Greece" & T6 == 31		//31 AHAIAS
				replace t10 = 5 if countryname == "Greece" & T6 == 32		//32 ILIAS (Elis)
				replace t10 = 5 if countryname == "Greece" & T6 == 33		//33 FTHIOTIDAS (Phthiotis)
				replace t10 = 1 if countryname == "Greece" & T6 == 34		//34 EVRYTANIAS
				replace t10 = 1 if countryname == "Greece" & T6 == 35		//35 PHOCIDOS
				replace t10 = 3 if countryname == "Greece" & T6 == 36		//36 VIOTIAS (Boeotia)
				replace t10 = 6 if countryname == "Greece" & T6 == 37		//37 EVIAS (Euboea)
				replace t10 = 15 if countryname == "Greece" & T6 == 38		//38 A ATHENS
				replace t10 = 44 if countryname == "Greece" & T6 == 39		//39 B ATHENS
				replace t10 = 6 if countryname == "Greece" & T6 == 40		//40 A PIRAEUS
				replace t10 = 8 if countryname == "Greece" & T6 == 41		//41 B PIRAEUS
				replace t10 = 15 if countryname == "Greece" & T6 == 42		//42 ATTICA
				replace t10 = 4 if countryname == "Greece" & T6 == 43		//43 KORINTHIAS
				replace t10 = 3 if countryname == "Greece" & T6 == 44		//44 ARGOLIDOS
				replace t10 = 3 if countryname == "Greece" & T6 == 45		//45 ARCADIAS
				replace t10 = 5 if countryname == "Greece" & T6 == 46		//46 MESSINIAS
				replace t10 = 3 if countryname == "Greece" & T6 == 47		//47 LAKONIAS
				replace t10 = 3 if countryname == "Greece" & T6 == 48		//48 LESVOU	(Lesbos)
				replace t10 = 2 if countryname == "Greece" & T6 == 49		//49 CHIOY (Chios)
				replace t10 = 1 if countryname == "Greece" & T6 == 50		//50 SAMOY
				replace t10 = 4 if countryname == "Greece" & T6 == 51		//51 KYKLADON (Cyclades)
				replace t10 = 5 if countryname == "Greece" & T6 == 52		//52 DODEKANISOU (Dodecanese)
				replace t10 = 4 if countryname == "Greece" & T6 == 53		//53 CHANION
				replace t10 = 2 if countryname == "Greece" & T6 == 54		//54 RETHYMNOY
				replace t10 = 8 if countryname == "Greece" & T6 == 55		//55 IRAKLIOU (Heraklion)
				replace t10 = 2 if countryname == "Greece" & T6 == 56		//56 LASSITHIOU			
				
			*get country number from survey 2 (Norway)
				replace t10 = 9 if countryname == "Norway" & T6 == 1		//1 Østfold
				replace t10 = 17 if countryname == "Norway" & T6 == 2		//2 Akershus
				replace t10 = 19 if countryname == "Norway" & T6 == 3		//3 Oslo
				replace t10 = 7 if countryname == "Norway" & T6 == 4		//4 Hedmark	
				replace t10 = 7 if countryname == "Norway" & T6 == 5		//5 Oppland	
				replace t10 = 9 if countryname == "Norway" & T6 == 6		//6 Buskerud	
				replace t10 = 7 if countryname == "Norway" & T6 == 7		//7 Vestfold	
				replace t10 = 6 if countryname == "Norway" & T6 == 8		//8 Telemark	
				replace t10 = 4 if countryname == "Norway" & T6 == 9		//9 Aust-Agder	
				replace t10 = 6 if countryname == "Norway" & T6 == 10		//10 Vest-Agder	
				replace t10 = 14 if countryname == "Norway" & T6 == 11		//11 Rogaland	
				replace t10 = 16 if countryname == "Norway" & T6 == 12		//12 Hordaland	
				replace t10 = 4 if countryname == "Norway" & T6 == 13		//13 Sogn og Fjordane	
				replace t10 = 9 if countryname == "Norway" & T6 == 14		//14 Møre og Romsdal	
				replace t10 = 10 if countryname == "Norway" & T6 == 15		//15 Sør-Trøndelag	
				replace t10 = 5 if countryname == "Norway" & T6 == 16		//16 Nord-Trøndelag
				replace t10 = 9 if countryname == "Norway" & T6 == 17		//17 Nordland	
				replace t10 = 6 if countryname == "Norway" & T6 == 18		//18 Troms	
				replace t10 = 5 if countryname == "Norway" & T6 == 19		//19 Finmark	
				
			*get country number from survey 2 (Iceland) 
				replace t10 = 10 if countryname == "Iceland" & t3 == 2013 & T6 == 1		//1. North East
				replace t10 = 8 if countryname == "Iceland" & t3 == 2013 & T6 == 2		//2. North West
				replace t10 = . if countryname == "Iceland" & t3 == 2013 & T6 == 3		//
				replace t10 = 10 if countryname == "Iceland" & t3 == 2013 & T6 == 4		//4. South
				replace t10 = 13 if countryname == "Iceland" & t3 == 2013 & T6 == 5		//5. South West
				replace t10 = 11 if countryname == "Iceland" & t3 == 2013 & T6 == 6		//6. Reykjavik South
				replace t10 = 11 if countryname == "Iceland" & t3 == 2013 & T6 == 7		//7. Reykjavik North

				replace t10 = 10 if countryname == "Iceland" & t3 == 2016 & T6 == 1		//1. North East
				replace t10 = 8 if countryname == "Iceland" & t3 == 2016 & T6 == 2		//2. North West
				replace t10 = . if countryname == "Iceland" & t3 == 2016 & T6 == 3		//
				replace t10 = 10 if countryname == "Iceland" & t3 == 2016 & T6 == 4		//4. South
				replace t10 = 13 if countryname == "Iceland" & t3 == 2016 & T6 == 5		//5. South West
				replace t10 = 11 if countryname == "Iceland" & t3 == 2016 & T6 == 6		//6. Reykjavik South
				replace t10 = 11 if countryname == "Iceland" & t3 == 2016 & T6 == 7		//7. Reykjavik North
					
				replace t10 = 11 if countryname == "Iceland" & t3 == 2017 & T6 == 1		//1. Reykjavik North
				replace t10 = 11 if countryname == "Iceland" & t3 == 2017 & T6 == 2		//2. Reykjavik South
				replace t10 = 13 if countryname == "Iceland" & t3 == 2017 & T6 == 3		//3. South West
				replace t10 = 8 if countryname == "Iceland" & t3 == 2017 & T6 == 4		//4. North West
				replace t10 = 10 if countryname == "Iceland" & t3 == 2017 & T6 == 5		//5. North East 
				replace t10 = 10 if countryname == "Iceland" & t3 == 2017 & T6 == 6		//6. South
				
			*Portugal (2015) 
				replace t10 = 5 if countryname == "Portugal" & t3 == 2015 & T6 == 1		//1 = Açores
				replace t10 = 16 if countryname == "Portugal" & t3 == 2015 & T6 == 2	//2 = Aveiro
				replace t10 = 3 if countryname == "Portugal" & t3 == 2015 & T6 == 3		//3 = Beja
				replace t10 = 19 if countryname == "Portugal" & t3 == 2015 & T6 == 4	//4 = Braga
				replace t10 = 3 if countryname == "Portugal" & t3 == 2015 & T6 == 5		//5 = Bragança
				replace t10 = 4 if countryname == "Portugal" & t3 == 2015 & T6 == 6		//6 = Castelo Branco
				replace t10 = 9 if countryname == "Portugal" & t3 == 2015 & T6 == 7		//7 = Coimbra
				replace t10 = 3 if countryname == "Portugal" & t3 == 2015 & T6 == 8		//8 = Évora
				replace t10 = 9 if countryname == "Portugal" & t3 == 2015 & T6 == 9		//9 = Faro
				replace t10 = 4 if countryname == "Portugal" & t3 == 2015 & T6 == 10	//10 = Guarda
				replace t10 = 10 if countryname == "Portugal" & t3 == 2015 & T6 == 11	//11 = Leiria
				replace t10 = 47 if countryname == "Portugal" & t3 == 2015 & T6 == 12	//12 = Lisboa
				replace t10 = 6 if countryname == "Portugal" & t3 == 2015 & T6 == 13	//13 = Madeira
				replace t10 = 2 if countryname == "Portugal" & t3 == 2015 & T6 == 14	//14 = Portalegre
				replace t10 = 39 if countryname == "Portugal" & t3 == 2015 & T6 == 15	//15 = Porto
				replace t10 = 9 if countryname == "Portugal" & t3 == 2015 & T6 == 16	//16 = Santarém
				replace t10 = 18 if countryname == "Portugal" & t3 == 2015 & T6 == 17	//17 = Setúbal
				replace t10 = 6 if countryname == "Portugal" & t3 == 2015 & T6 == 18	//18 = Viana do Castelo
				replace t10 = 5 if countryname == "Portugal" & t3 == 2015 & T6 == 19	//19 = Vila Real
				replace t10 = 9 if countryname == "Portugal" & t3 == 2015 & T6 == 20	//20 = Viseu
				replace t10 = 2 if countryname == "Portugal" & t3 == 2015 & T6 == 21	//21 = Europe
				replace t10 = 2 if countryname == "Portugal" & t3 == 2015 & T6 == 22	//22 = Rest of the world	
					
			*Great britain
				replace t10 = 1 if countryname == "Great Britain" & t10 == .	
	
	*get log of dm
		gen dm = t10
		gen dm2 = ln(dm)				
	
	*gen MP variable 
		gen MP = 1 if a4b1 == 1 
	
	*gender variable
		replace e1 = E1 if wave == 2
		gen female = e1
		replace female = female - 1		
		
**************
***ANALYSIS***	
**************
	
	*Table 2
		egen country_wave = group(wave countryname)
		sort t0	
		meoprobit pork c.dm2##i.closed personalism if MP == 1 || country_wave:				
		meoprobit pork c.dm2##i.closed personalism female leftright government coalition federalism clientelism if MP == 1  || country_wave:
		meoprobit pork c.dm2##i.closed personalism female leftright government coalition federalism clientelism seniority if MP == 1  || country_wave:
	
	*Figure 
		meoprobit pork c.dm2##i.closed personalism if MP == 1 || country_wave:				
	
	*set up variables that we will use during the loops
		gen pred1 = .
		gen pred2 = .
		gen pred3 = .
		gen pred4 = .
		gen pred5 = .
		
		capture drop xvalue
		gen xvalue = . 
		
		*get predictions
			local a = 0.69314718			//dm = 2 to make the figure for multimember districts only. 
			local b = 1
				
			while `a' <= 5.0106353 {
				
			replace pred1 = normprob(_b[/cut1] - (_b[dm2]*`a' + _b[personalism]*3)) in `b'
			replace pred2 = (normprob(_b[/cut2] - (_b[dm2]*`a' + _b[personalism]*3))) - (normprob(_b[/cut1] - (_b[dm2]*`a' + _b[personalism]*3))) in `b'
			replace pred3 = (normprob(_b[/cut3] - (_b[dm2]*`a' + _b[personalism]*3))) - (normprob(_b[/cut2] - (_b[dm2]*`a' + _b[personalism]*3))) in `b'
			replace pred4 = (normprob(_b[/cut4] - (_b[dm2]*`a' + _b[personalism]*3))) - (normprob(_b[/cut3] - (_b[dm2]*`a' + _b[personalism]*3))) in `b'
			replace pred5 = 1-(normprob(_b[/cut4] - (_b[dm2]*`a' + _b[personalism]*3))) in `b'		
			replace xvalue = `a' in `b'
			local a = `a' + 0.01				
			local b = `b' + 1			
		
			}		
					
		*Get cumultative predicted probabilities 	
			replace pred2 = pred1 + pred2
			replace pred3 = pred2 + pred3
			replace pred4 = pred3 + pred4
			replace pred5 = pred4 + pred5
			
		*de-log dm
			replace xvalue = exp(xvalue) 
			
		*gen figure
			twoway (area pred1 xvalue,  lcolor(black)) (rarea pred1 pred2 xvalue,  lcolor(black)) (rarea pred2 pred3 xvalue,  lcolor(black)) (rarea pred3 pred4 xvalue,  lcolor(black)) (rarea pred4 pred5 xvalue,  lcolor(black)), legend(order(1 "No emphasis" 2 "Not much emphasis" 3 "Some emphasis" 4 "Much emphasis" 5 "Very much emphasis")) name(tempg1, replace) graphregion(color(white)) ytitle("Cumulative probability") xlabel(0(25)150) ylab(0(0.2)1, nogrid) xtitle("District magnitude") 
				
